package org.hit.burkun.extract;

import java.util.HashMap;
import java.util.Map;

import org.hit.burkun.entities.realtions.BaseRealtion;

public class FiledsToRealtionMapper {
	public enum FILED {
		ENT1, ENT2, SCORE, DBID, DESC, CALC_METHOD, EVIDENCE_URLS, OTHER
	};

	private String tbName;
	private Map<FILED, String> fileds;
	private String queryTemplete;
	private BaseRealtion.TYPE type;
	private String desc;
	private Map<FILED, Integer> resIndex = new HashMap<FiledsToRealtionMapper.FILED, Integer>();
	private boolean isDirect = false;

	public FiledsToRealtionMapper(String tbName, Map<FILED, String> fileds,
			BaseRealtion.TYPE type) {
		super();
		this.tbName = tbName;
		this.fileds = fileds;
		this.type = type;
		this.init();
	}

	private void init() {
		StringBuilder sb = new StringBuilder();
		int idxer = 0;
		sb.append("select ").append(fileds.get(FILED.ENT1)).append(",");
		resIndex.put(FILED.ENT1, idxer++);
		sb.append(fileds.get(FILED.ENT2)).append(",");
		resIndex.put(FILED.ENT2, idxer++);
		if (fileds.containsKey(FILED.SCORE)) {
			sb.append(fileds.get(FILED.SCORE)).append(",");
			resIndex.put(FILED.SCORE, idxer++);
		}
		if (fileds.containsKey(FILED.DBID)) {
			sb.append(fileds.get(FILED.DBID)).append(",");
			resIndex.put(FILED.DBID, idxer++);
		}
		if (fileds.containsKey(FILED.DESC)) {
			sb.append(fileds.get(FILED.DESC)).append(",");
			resIndex.put(FILED.DESC, idxer++);
		}
		if (fileds.containsKey(FILED.CALC_METHOD)) {
			sb.append(fileds.get(FILED.CALC_METHOD)).append(",");
			resIndex.put(FILED.CALC_METHOD, idxer++);
		}
		if (fileds.containsKey(FILED.EVIDENCE_URLS)) {
			sb.append(fileds.get(FILED.EVIDENCE_URLS)).append(",");
			resIndex.put(FILED.EVIDENCE_URLS, idxer++);
		}
		if (fileds.containsKey(FILED.OTHER)) {
			sb.append(fileds.get(FILED.OTHER)).append(",");
			resIndex.put(FILED.OTHER, idxer++);
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append(" from ").append(tbName);
		queryTemplete = sb.toString();
	}

	public String getQueryTemplete() {
		return queryTemplete;
	}

	public void setQueryTemplete(String queryTemplete) {
		this.queryTemplete = queryTemplete;
	}

	public BaseRealtion.TYPE getType() {
		return type;
	}

	public int getResultIdxByFiled(FILED fd) {
		if (resIndex.containsKey(fd)) {
			return resIndex.get(fd);
		}
		return -1;
	}

	public int getFiledsSize() {
		return fileds.size();
	}

	public void setDirect(boolean direct) {
		this.isDirect = direct;
	}

	public boolean isDirect() {
		return isDirect;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public void setWherecondition(String wherecondition) {
		this.queryTemplete += " " + wherecondition;
	}

}
